<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入axios文件 -->
    <script src="./node_modules/axios/dist/axios.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 30px;
        }

        a {
            text-decoration: none;
        }

        table,
        td,
        th {
            border: 1px solid black;
            border-spacing: 0;
            border-collapse: collapse;
        }

        th,
        td {
            padding: 5px 10px;
        }
    </style>
</head>

<body>
    <h1>用户列表</h1>
    <a href="./add.html">添加用户</a>
    <br><br>
    <table>
        <tr>
            <th>id</th>
            <th>用户名</th>
            <th>密码</th>
            <th>操作</th>
        </tr>
    </table>
    <script>
        const table = document.querySelector('table')
        axios.get('http://localhost:3000/users').then(res => {
            var arr = res.data
            table.innerHTML = arr.map(item => {
                return `<tr>
                        <td>${item.id}</td>
                        <td>${item.username}</td>
                        <td>${item.password}</td>
                        <td>
                             <a href="./edit.html?id=${item.id}" class="a1">编辑</a>
                             <a href="" onclick= 'deleteItem(${item.id})''>删除</a>
                         </td>
                             </tr>`
            }).join('')
        })

        function deleteItem(id){
            var res = confirm('你确定要删除吗')
            if(res){
                axios.delete(`http://localhost:3000/users/${id}`).then(res=>{
                    location.href = './index.html'
                })
            }
        }
    </script>
</body>

</html>